class TFCMGun extends TFWeapon;

var(Gfx) float testX;
var(Gfx) float testY;

var() texture DotMat;

var(Gfx) float borderX;
var(Gfx) float borderY;

var(Gfx) float focusX;
var(Gfx) float focusY;
var(Gfx) float innerArrowsX;
var(Gfx) float innerArrowsY;
var(Gfx) Color ArrowColor;
var(Gfx) color FocusColor;
var(Gfx) color DotColor;
var(Gfx) color Dot2Color;
var(Gfx) color AimDotColor;

var transient float LastFOV;
var() bool zoomed;

simulated event RenderOverlays( Canvas Canvas )
{
	local float tileScaleX;
	local float tileScaleY;
	local float fX;
	local float fY;
//	local float charge;
	local float tX;
	local float tY;
	local int m;

	local pawn p;
	local vector vecPawnView;
	local float dist;
	local vector X, Y, Z, L, V;

    if ((Hand < -1.0) || (Hand > 1.0))
    {
		for (m = 0; m < NUM_FIRE_MODES; m++)
		{
			if (FireMode[m] != None)
			{
				FireMode[m].DrawMuzzleFlash(Canvas);
			}
		}
	}

	if ( PlayerController(Instigator.Controller) == None )
	{
        Super.RenderOverlays(Canvas);
		zoomed=false;
		return;
	}

    if ( LastFOV > PlayerController(Instigator.Controller).DesiredFOV )
    {
        PlaySound(Sound'TFMedia.MarksMan.MMZoomIn', SLOT_Misc,,,,,false);
    }
    else if ( LastFOV < PlayerController(Instigator.Controller).DesiredFOV )
    {
        PlaySound(Sound'TFmedia.MarksMan.MMZoomOut', SLOT_Misc,,,,,false);
    }
    LastFOV = PlayerController(Instigator.Controller).DesiredFOV;

    if ( (PlayerController(Instigator.Controller).DesiredFOV == PlayerController(Instigator.Controller).DefaultFOV)
		|| (Level.bClassicView && (PlayerController(Instigator.Controller).DesiredFOV == 90)) )
	{
        Super.RenderOverlays(Canvas);
		zoomed=false;
	}
	else
    {
		//AIM-DOTS

        GetAxes(instigator.GetViewRotation(), X, Y, Z);

		foreach DynamicActors(Class'pawn', p)
		{
			vecPawnView = p.Location - instigator.Location - (instigator.EyeHeight * vect(0,0,1));

			if ( (p.playerreplicationinfo==none || p.playerreplicationinfo.team.teamindex!=instigator.playerreplicationinfo.team.teamindex) && (vecPawnView dot X) > 0.0 && FastTrace(p.location, instigator.Location+(instigator.EyeHeight*Vect(0,0,1))))
			{
				L = p.Location;
				dist = vsize(L-instigator.Location+(instigator.EyeHeight*Vect(0,0,1)));
				L += (dist/10000)*p.Velocity;
				V = playercontroller(instigator.controller).Player.Console.WorldToScreen(L, instigator.Location + (instigator.EyeHeight * Vect(0,0,1)), instigator.Rotation);
            	fx = V.X;
				fy = V.Y;

				Canvas.DrawColor = AimdotColor;
				Canvas.SetPos(fx - 1.5 * DotMat.USize, fy - 1.5 * DotMat.VSize);
				Canvas.DrawTile(DotMat,
				    3.0 * DotMat.USize,
				    3.0 * DotMat.VSize,
				    0.0,
				    0.0,
				    DotMat.USize,
				    DotMat.VSize);

                L = p.Location;
            	L += (dist/10000)*(p.Velocity/2);
                V = playercontroller(instigator.controller).Player.Console.WorldToScreen(L, instigator.Location + (instigator.EyeHeight * Vect(0,0,1)), instigator.Rotation);
            	fx = V.X;
				fy = V.Y;

            	Canvas.DrawColor = Dot2Color;
				Canvas.SetPos(fx - 1.0 * DotMat.USize, fy - 1.0 * DotMat.VSize);
				Canvas.DrawTile(DotMat,
				    2.0 * DotMat.USize,
				    2.0 * DotMat.VSize,
				    0.0,
				    0.0,
				    DotMat.USize,
				    DotMat.VSize);

				L = p.Location;
            	V = playercontroller(instigator.controller).Player.Console.WorldToScreen(L, instigator.Location + (instigator.EyeHeight * Vect(0,0,1)), instigator.Rotation);
            	fx = V.X;
				fy = V.Y;

            	Canvas.DrawColor = DotColor;
				Canvas.SetPos(fx - 0.75 * DotMat.USize, fy - 0.75 * DotMat.VSize);
				Canvas.DrawTile(DotMat,
				    1.5 * DotMat.USize,
				    1.5 * DotMat.VSize,
				    0.0,
				    0.0,
				    DotMat.USize,
				    DotMat.VSize);
			}
		}


        //ZOOM

        tileScaleX = Canvas.SizeX / 640.0f;
		tileScaleY = Canvas.SizeY / 480.0f;

		fX = 2*focusX * tileScaleX;
		fY = 2*focusY * tileScaleX;

		tX = testX * tileScaleX;
		tY = testY * tileScaleX;

        //SetZoomBlendColor(Canvas);

        //Canvas.Style = 255;
		//Canvas.SetPos(0,0);
        //Canvas.DrawTile( Material'ZoomFB', Canvas.SizeX, Canvas.SizeY, 128, 128, 256, 256 ); // !! hardcoded size

		Canvas.DrawColor = FocusColor;
        Canvas.DrawColor.A = 255; // 255 was the original -asp. WTF??!?!?!
		Canvas.Style = ERenderStyle.STY_Alpha;

		Canvas.SetPos((Canvas.SizeX*0.5)-fX,(Canvas.SizeY*0.5)-fY);
		Canvas.DrawTile( Texture'SniperFocus', fX*2.0, fY*2.0, 0.0, 0.0, Texture'SniperFocus'.USize, Texture'SniperFocus'.VSize );

        fX = innerArrowsX * tileScaleX;
		fY = innerArrowsY * tileScaleY;

        Canvas.DrawColor = ArrowColor;
        Canvas.SetPos((Canvas.SizeX*0.5)-fX,(Canvas.SizeY*0.5)-fY);
		Canvas.DrawTile( Texture'SniperArrows', fX*2.0, fY*2.0, 0.0, 0.0, Texture'SniperArrows'.USize, Texture'SniperArrows'.VSize );

		zoomed = true;
	}
}

simulated function SetZoomBlendColor(Canvas c)
{
    local Byte    val;
    local Color   clr;
    local Color   fog;

    clr.R = 255;
    clr.G = 255;
    clr.B = 255;
    clr.A = 255;

    if( Instigator.Region.Zone.bDistanceFog )
    {
        fog = Instigator.Region.Zone.DistanceFogColor;
        val = 0;
        val = Max( val, fog.R);
        val = Max( val, fog.G);
        val = Max( val, fog.B);

        if( val > 128 )
        {
            val -= 128;
            clr.R -= val;
            clr.G -= val;
            clr.B -= val;
        }
    }
    c.DrawColor = clr;
}

simulated function ClientWeaponThrown()
{
    if( (Instigator != None) && (PlayerController(Instigator.Controller) != None) )
        PlayerController(Instigator.Controller).EndZoom();
    Super.ClientWeaponThrown();
}

simulated function ClientStartFire(int mode)
{
    if (mode == 1 && instigator.bIsCrouched)
    {
        FireMode[mode].bIsFiring = true;
        if( Instigator.Controller.IsA( 'PlayerController' ) )
            PlayerController(Instigator.Controller).ToggleZoom();
    }
    else
    {
        Super.ClientStartFire(mode);
    }
}

simulated function ClientStopFire(int mode)
{
    if (mode == 1 && instigator.bIsCrouched)
    {
        FireMode[mode].bIsFiring = false;
        if( Instigator.Controller.IsA( 'PlayerController' ) )
            PlayerController(Instigator.Controller).StopZoom();
    }
    else
    {
        Super.ClientStopFire(mode);
    }
}

simulated function bool PutDown()
{
    if( Instigator.Controller.IsA( 'PlayerController' ) )
        PlayerController(Instigator.Controller).EndZoom();
    if ( Super.PutDown() )
    {
		GotoState('');
		return true;
	}
	return false;
}

simulated function bool WeaponCentered()
{
	return ( bSpectated || (Hand > 1) || (Hand == 0) );
}

simulated function DrawWeaponInfo(Canvas Canvas)
{
	NewDrawWeaponInfo(Canvas, 0.705*Canvas.ClipY);
}

simulated function NewDrawWeaponInfo(Canvas Canvas, float YPos)
{
	local int i,Count;
	local float ScaleFactor;

	ScaleFactor = 99 * Canvas.ClipX/3200;
	Canvas.Style = ERenderStyle.STY_Alpha;
	Canvas.DrawColor = class'HUD'.Default.WhiteColor;
	Count = Min(8,AmmoAmount(1));
    for( i=0; i<Count; i++ )
    {
		Canvas.SetPos(Canvas.ClipX - (0.5*i+1) * ScaleFactor, YPos);
		Canvas.DrawTile( Material'HudContent.Generic.HUD', ScaleFactor, ScaleFactor, 174, 259, 46, 45);
	}
	if ( AmmoAmount(1) > 8 )
	{
		Count = Min(16,AmmoAmount(1));
		for( i=8; i<Count; i++ )
		{
			Canvas.SetPos(Canvas.ClipX - (0.5*(i-8)+1) * ScaleFactor, YPos - ScaleFactor);
			Canvas.DrawTile( Material'HudContent.Generic.HUD', ScaleFactor, ScaleFactor, 174, 259, 46, 45);
		}
	}
}

function byte BestMode()
{
	if ( (FRand() < 0.1) && (Ammo[1].AmmoAmount >= FireMode[1].AmmoPerFire) )
		return 1;
    if ( Ammo[0].AmmoAmount >= FireMode[0].AmmoPerFire )
		return 0;
	return 1;
}

defaultproperties
{
     testX=100.000000
     testY=100.000000
     DotMat=Texture'Crosshairs.HUD.Crosshair_Dot'
     borderX=60.000000
     borderY=60.000000
     focusX=135.000000
     focusY=105.000000
     innerArrowsX=20.000000
     innerArrowsY=20.000000
     ArrowColor=(G=110,R=150,A=255)
     FocusColor=(R=255,A=215)
     DotColor=(R=200,A=255)
     Dot2Color=(G=128,R=128,A=255)
     AimDotColor=(B=25,G=255,R=25,A=255)
     FireModeClass(0)=Class'taskforces.TFCMFire'
     FireModeClass(1)=Class'taskforces.TFCMAltFire'
     SelectAnim="Pickup"
     PutDownAnim="PutDown"
     SelectSound=Sound'TFMedia.Commando.TFCMslct'
     SelectForce="SwitchToShockRifle"
     AIRating=0.630000
     CurrentRating=0.630000
     EffectOffset=(X=65.000000,Y=14.000000,Z=-10.000000)
     DisplayFOV=60.000000
     HudColor=(B=100,R=128)
     SmallViewOffset=(X=2.000000,Y=9.000000,Z=-8.000000)
     CenteredOffsetY=-8.000000
     CenteredYaw=-500
     InventoryGroup=5
     GroupOffset=2
     PickupClass=Class'XWeapons.ShockRiflePickup'
     PlayerViewOffset=(X=-50.000000,Z=5.000000)
     PlayerViewPivot=(Pitch=1000,Yaw=-800,Roll=-500)
     BobDamping=1.800000
     AttachmentClass=Class'taskforces.TFCMAttach'
     IconMaterial=Texture'HUDContent.Generic.HUD'
     IconCoords=(X1=250,Y1=110,X2=330,Y2=145)
     ItemName="Commando Rifle"
     LightType=LT_Steady
     LightEffect=LE_NonIncidence
     LightHue=32
     LightSaturation=222
     LightBrightness=150.000000
     LightRadius=4.000000
     LightPeriod=3
     Mesh=SkeletalMesh'Weapons.ShockRifle_1st'
     DrawScale3D=(X=2.300000,Y=1.500000,Z=2.100000)
     Skins(0)=Texture'TFMedia.Commando.tfCMGunTex0'
     Skins(1)=Texture'TFMedia.Commando.tfCMGunTex0'
     Skins(2)=Shader'TFMedia.Defender.tfDFSphereTex'
     UV2Texture=Shader'XGameShaders.WeaponShaders.WeaponEnvShader'
     AmmoRegenAmount(0)=5
     SkillCost=200
}
